﻿@{
    ViewBag.Title = "AllowAPPAuthorize";
    Layout = "~/Views/Shared/_Form.cshtml";
}
<script type="text/javascript">
    var userId = request('userId');
    var value = request('AuthorizeDataType');
    $(function () {
        GetModuleTree();

        //权限最末级
        $("#AuthorizeDateType").ComboBox({
            data: [{ "key": 0, "value": "机构" }, { "key": 1, "value": "小区" }, { "key": 2, "value": "楼栋" }, { "key": 4, "value": "楼层" }],
            id: "key",
            text: "value",
            description: "==选择权限层级==",
            click: function (e) {
                debugger;
                if (e.attr('data-value') == "") {
                    value = -1;
                    $("#OrganizePStructTree").attr('display', 'none');
                }
                else if (e.attr('data-value') != value) {
                    $("#OrganizePStructTree").attr('display', 'block');
                    value = parseInt(e.attr('data-value'));
                    GetOrganizePstructTree(value);
                }
            }
        });
        if (value != "") {
            // $("#OrganizePStructTree").attr('disabled', 'disabled');
            $("#AuthorizeDateType").ComboBoxSetValue(value);
            GetOrganizePstructTree(parseInt(value));
        }
    })

    function GetModuleTree() {
        var item = {
            height: 500,
            showcheck: true,
            url: "../../AuthorizeManage/PermissionUser/APPModuleTreeJson?userId=" + userId
        };
        $("#ModuleTree").treeview(item);
    }

    //获取机构楼盘数据架构
    function GetOrganizePstructTree(type) {
        $.ajax({
            url: "../../AuthorizeManage/PermissionUser/OrganizePstructTreeJson?userId=" + userId + "&AuthorizeDataType=" + type,
            type: "GET",
            dataType: "json",
            async: false,
            success: function (data) {
                
                var $treeJson = data.treeJson;
                var $authorizeType = data.authorizeType;
                var $authorizeData = data.authorizeData;
                var item = {
                    height: 500,
                    showcheck: true,
                    data: JSON.parse($treeJson),
                };
                $("#OrganizePStructTree").treeview(item);
                $("#OrganizePStructTree").find('li.bbit-tree-node').each(function () {
                    var $li = $(this);
                    $li.css({ position: 'relative' });
                    debugger;
                    var _data_value = $li.find('a').find('span').attr('data-value');
                    var _data_title = $('#OrganizePStructTree_' + _data_value.replace(/-/g, "_")).attr('title');
                    var _html = '<div style="position: absolute;display:none;right: 0px;top:4px;z-index: 1;"><div class="checkbox">';
                    _html += '<label><input name="' + _data_value + '" type="checkbox" value="' + _data_title + '" /></label>';
                    _html += '</div></div>';
                    $li.append(_html);
                });
            }
        });
    }

    //保存表单
    function AcceptClick() {
        if (!$('#form1').Validform()) {
            return false;
        }
        var postData = $("#form1").GetWebControls();
        postData["userId"] = userId;
        postData["moduleIds"] = String($("#ModuleTree").getCheckedAllNodes());
        postData["AuthorizeDataType"] = value;
        postData["authorizeDataJson"] = JSON.stringify(GetDataAuthorize());
        $.SaveForm({
            url: "../../AuthorizeManage/PermissionUser/SaveAuthorizeAppModule",
            param: postData,
            loading: "正在保存用户授权...",
            success: function () {
                $.currentIframe().$("#gridTable").trigger("reloadGrid");
            }
        })
    }

    //获取数据范围权限选中值、返回Json
    function GetDataAuthorize() {
        var dataAuthorize = [];
        var authorizeType = $("input[name='authorizeType']:checked").val();    
        var selectedData = $("#OrganizePStructTree").getCheckedAllNodes();
        for (var i = 0; i < selectedData.length; i++) {
            var ResourceId = selectedData[i];          
            var ItemType = $("input[name='" + ResourceId + "']").val();
            var rowdata = {
                ResourceId: ResourceId,
                ItemType: ItemType,
                AuthorizeType: 5
            }
            dataAuthorize.push(rowdata);
        }
        return dataAuthorize;
    }
 </script>
    <div style="margin-left: 10px; margin-right: 10px;">
        <ul class="nav nav-tabs">
            <li class="active"><a href="#BaseInfo" data-toggle="tab">系统功能</a></li>
            <li><a href="#AuthorizeData" data-toggle="tab">数据权限</a></li>
            @*<li><a href="#ExpandInfo" data-toggle="tab">扩展属性</a></li>*@
        </ul>
        <div class="tab-content" style="padding-top: 15px;">
            <div id="BaseInfo" class="tab-pane active" style=" padding-right: 30px;">               
                <div id="ModuleTree" style="margin: 10px;"></div>
            </div>
            <div id="AuthorizeData" class="tab-pane " style=" padding-right: 30px;">
                <ul>
                    <li class="list-group-item">
                        <div>
                            <label style="color:darkred">
                                数据权限验证层级:
                            </label>
                            <div id="AuthorizeDateType" type="selectTree " class="ui-select"></div>
                        </div>
                        <div id="OrganizePStructTree" style="margin: 10px; margin-bottom: 0px;"></div>
                    </li>
                </ul>              
            </div>
            @*<div id="ExpandInfo" class="tab-pane " style=" padding-right: 30px;">
            </div>*@
        </div>
    </div>
